package org.jeecg.common.util;

import org.apache.commons.lang3.StringUtils;

/**
 * 流水号生成规则(按默认规则递增，数字从1-99开始递增，数字到99，递增字母;位数不够增加位数)
 * A001
 * A001A002
 *
 * @Author zhangdaihao
 */
public class AutoCodingCodeUtil {

    // 数字位数(默认生成4位的数字)
    private static final int DEFAULT_NUM_LENGTH = 4;

    /**
     * 将数字前面位数补零
     *
     * @param num
     * @return
     */
    public static String getNextStrNum(Integer num) {
        return getNextStrNum(num, DEFAULT_NUM_LENGTH);
    }

    public static String getNextStrNum(Integer num, Integer numLength) {
        if (numLength == null){
            numLength = DEFAULT_NUM_LENGTH;
        }
        if (num == null) {
            num = 0;
        }
        return getStrNum(getNextNum(num), numLength);
    }

    public static String getNextStrNum(String numStr) {
        return getNextStrNum(numStr, DEFAULT_NUM_LENGTH);
    }

    public static String getNextStrNum(String numStr, Integer numLength) {
        if (numLength == null){
            numLength = DEFAULT_NUM_LENGTH;
        }
        Integer num = 0;
        if (StringUtils.isNotBlank(numStr)) {
            try {
                num = Integer.valueOf(numStr);
            } catch (NumberFormatException e) {
                num = 0;
            }
        }
        return getNextStrNum(num, numLength);
    }

    public static String getNextEN(String en) {
        return getNextEN(en, DEFAULT_NUM_LENGTH);
    }

    public static String getNextEN(String en, Integer numLength) {
        if (numLength == null){
            numLength = DEFAULT_NUM_LENGTH;
        }
        en = en.toUpperCase();
        if (StringUtils.isBlank(en)){
            en = "A";
        } else {
            char[] chars = en.toCharArray();
            for (int i = chars.length - 1; i >= 0; i--) {
                if (chars[i] != 'Z'){
                    chars[i] = getNextEn(chars[i]);
                    break;
                }
                chars[i] = getNextEn(chars[i]);
            }
            en = String.valueOf(chars);
        }
        return getNextEN(en, numLength);
    }

    /**
     * 将数字前面位数补零
     *
     * @param num
     * @return
     */
    public static String getStrNum(Integer num, int numLength) {
        if (num == null) {
            num = 1;
        }
        String s = String.valueOf(num);
        if (s.length() > numLength) {
            return s.substring(s.length() - numLength);
        }
        return String.format("%0" + numLength + "d", num);
    }

    public static String getStrNum(Integer num) {
        return getStrNum(num, DEFAULT_NUM_LENGTH);
    }

    /**
     * 递增获取下个数字
     *
     * @param num
     * @return
     */
    public static int getNextNum(int num) {
        return num + 1;
    }

    /**
     * 递增获取下个字母
     *
     * @param zimu
     * @return
     */
    public static char getNextEn(char zimu) {
        if (zimu == 'Z') {
            return 'A';
        }
        zimu++;
        return zimu;
    }

    /**
     * 字母设置长度，如果不够A占位
     * @param en
     * @param numLength
     * @return
     */
    public static String getStrEn(String en, int numLength) {
        if (StringUtils.isBlank(en)){
            en = "A";
        }
        if (en.length() > numLength) {
            return en.substring(en.length() - numLength);
        }
        return String.format("%A" + numLength + "s", en);
    }
}
